package com.aidex.system.controller;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.AjaxResult;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.model.LoginUser;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.ServletUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.domain.CargoDetail;
import com.aidex.system.domain.vo.CargoDetailApply;
import com.aidex.system.service.CargoDetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 货品清单Controller
 * @author jmy
 * @email jmyinjg@163.com
 * @date 2021-10-06
 */
@RestController
@RequestMapping("/system/cargoDetail")
public class CargoDetailController extends BaseController
{
    @Autowired
    private CargoDetailService cargoDetailService;

    /**
     * 查询货品清单列表
     */
//    @PreAuthorize("@ss.hasPermi('system:cargoDetail:list')")
    @GetMapping("/list")
    public R<PageInfo> list(CargoDetail cargoDetail, HttpServletRequest request, HttpServletResponse response)
    {
        cargoDetail.setPage(new PageDomain(request, response));
        return R.data(cargoDetailService.findPage(cargoDetail));
    }

    /**
     * 获取货品清单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:cargoDetail:query')")
    @GetMapping(value = "/{id}")
    public R<CargoDetail> detail(@PathVariable("id") String id)
    {
        return R.data(cargoDetailService.get(id));
    }

    /**
     * 新增货品清单
     */
//    @PreAuthorize("@ss.hasPermi('system:cargoDetail:add')")
    @Log(title = "货品清单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  CargoDetail cargoDetail)
    {
        cargoDetail.setSource(1);
        cargoDetail.setStatus(0);
        return R.status(cargoDetailService.save(cargoDetail));
    }

    /**
     * 修改货品清单
     */
//    @PreAuthorize("@ss.hasPermi('system:cargoDetail:edit')")
    @Log(title = "货品清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CargoDetail cargoDetail)
    {
        return R.status(cargoDetailService.save(cargoDetail));
    }


    /**
     * 删除货品清单
     */
//    @PreAuthorize("@ss.hasPermi('system:cargoDetail:remove')")
    @Log(title = "货品清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(cargoDetailService.deleteCargoDetailByIds(ids));
    }


    /**
     * 导出货品清单列表
     */
//    @PreAuthorize("@ss.hasPermi('system:cargoDetail:export')")
    @Log(title = "货品清单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CargoDetail cargoDetail)
    {
        List<CargoDetail> list = cargoDetailService.findList(cargoDetail);
        ExcelUtil<CargoDetail> util = new ExcelUtil<CargoDetail>(CargoDetail.class);
        return util.exportExcel(list, "cargoDetail");
    }

    /**
     *
     * @param file
     * @param type
     * @return
     * @throws Exception
     */
    @Log(title = "货品清单", businessType = BusinessType.IMPORT)
    @PostMapping("{type}/importData")
    public AjaxResult importData(MultipartFile file, @PathVariable("type") Integer type) throws Exception
    {
        ExcelUtil<CargoDetail> util = new ExcelUtil<>(CargoDetail.class);
        List<CargoDetail> details = util.importExcel(file.getInputStream());
        for (CargoDetail detail : details) {
            // 判断是否存在
            if (cargoDetailService.getByName(detail.getKeywords()) != null) {
                continue;
            }
            detail.setType(type);
            detail.setSource(1);
            detail.setStatus(0);
            cargoDetailService.save(detail);
        }
        return AjaxResult.success();
    }

    @GetMapping("/importTemplate")
    public R importTemplate()
    {
        ExcelUtil<CargoDetail> util = new ExcelUtil<>(CargoDetail.class);
        return util.importTemplateExcel("清单数据");
    }

    /**
     * 审核货品清单
     * @param apply
     * @return
     */
    @PostMapping("apply")
    public R apply(@RequestBody CargoDetailApply apply) {
        return R.status(cargoDetailService.apply(apply));
    }

}
